<?php
// 应用公共文件
function douyinPaySign($map) {
    $rList = [];
    foreach($map as $k =>$v) {
        if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id")
            continue;

        $value = trim(strval($v));
        if (is_array($v)) {
            $value = arrayToStr($v);
        }

        $len = strlen($value);
        if ($len > 1 && substr($value, 0,1)=="\"" && substr($value,$len, $len-1)=="\"")
            $value = substr($value,1, $len-1);
        $value = trim($value);
        if ($value == "" || $value == "null")
            continue;
        $rList[] = $value;
    }
    $rList[] = "7x6t4tqKwkVChApTA8CSWLMUBofi5lormLb3BzNd";
    sort($rList, SORT_STRING);
    return md5(implode('&', $rList));
}

function arrayToStr($map) {
    $isMap = isArrMap($map);

    $result = "";
    if ($isMap){
        $result = "map[";
    }

    $keyArr = array_keys($map);
    if ($isMap) {
        sort($keyArr);
    }

    $paramsArr = array();
    foreach($keyArr as  $k) {
        $v = $map[$k];
        if ($isMap) {
            if (is_array($v)) {
                $paramsArr[] = sprintf("%s:%s", $k, arrayToStr($v));
            } else  {
                $paramsArr[] = sprintf("%s:%s", $k, trim(strval($v)));
            }
        } else {
            if (is_array($v)) {
                $paramsArr[] = arrayToStr($v);
            } else  {
                $paramsArr[] = trim(strval($v));
            }
        }
    }

    $result = sprintf("%s%s", $result, join(" ", $paramsArr));
    $result = sprintf("[%s]", $result);

    return $result;
}

function isArrMap($map) {
    foreach($map as $k =>$v) {
        if (is_string($k)){
            return true;
        }
    }

    return false;
}